<?php

namespace app\admin\controller\weixin;

use app\common\controller\AdminController;
use app\common\model\PointReceive;
use app\common\model\WeixinExpress;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="微信用户管理")
 */
class User extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\WeixinUser();

        $this->assign('getTypeList', $this->model->getTypeList());

        $this->assign('getStatusList', $this->model->getStatusList());
        $this->field = "*,openid as get_integer,openid as get_express";
    }

    public function add()
    {
    }

    public function edit($id)
    {
    }

    /**
     * @NodeAnotation (title="赠送积分")
     * @return void
     * @throws \think\Exception
     */
    public function send($id)
    {
        $row = $this->model->getBaseById($id);
        if (empty($row)) {
            $this->error("用户不存在");
        }
        if ($this->request->isPost()) {
            $number = $this->request->param('number', 0, 'intval');
            if ($number == 0) {
                $this->error("请输入正确的赠送数量");
            }
            $res = (new PointReceive())->insertSaveById([
                "type" => 3,
                "source_type" => 3,
                "number" => $number,
                "openid" => $row['openid']
            ]);
            if ($res) {
                $this->success("赠送成功");
            } else {
                $this->error("赠送失败");
            }
        }
        return $this->fetch('', [
            "row" => $row

        ]);
    }

    /**
     * @NodeAnotation(title="赠送快递次数")
     * @param $id
     * @return void
     * @throws \think\Exception
     */
    public function push($id)
    {
        $row = $this->model->getBaseById($id);
        if (empty($row)) {
            $this->error("用户不存在");
        }
        if ($this->request->isPost()) {
            $number = $this->request->param('number', 0, 'intval');
            if ($number == 0) {
                $this->error("请输入正确的赠送数量");
            }
            $res = (new WeixinExpress())->insertSaveById([
                "num" => $number,
                "openid" => $row['openid'],
                "source" => 4
            ]);
            if ($res) {
                $this->success("赠送成功");
            } else {
                $this->error("赠送失败");
            }
        }
        return $this->fetch('', [
            "row" => $row

        ]);
    }
}